//
//  DnpContactManager.m
//  dnp_plugin
//
//

#import "DnpContactManager.h"

@implementation DnpContactManager

+ (void)chooseContactsCompletion:(void (^)(NSArray<NSDictionary *> * _Nonnull))completion {
    NSArray *keyList = @[
        CNContactGivenNameKey,
        CNContactFamilyNameKey,
        CNContactPhoneNumbersKey,
        CNContactBirthdayKey,
        CNContactEmailAddressesKey
    ];
    
    CNContactFetchRequest *contactReq = [[CNContactFetchRequest alloc] initWithKeysToFetch:keyList];
    CNContactStore *contactStore = [[CNContactStore alloc] init];
    
    NSMutableArray<NSDictionary *> *contactsArray = [NSMutableArray array];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        @try {
            [contactStore enumerateContactsWithFetchRequest:contactReq error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
                NSString *beersheba = [[NSString stringWithFormat:@"%@ %@", contact.givenName, contact.familyName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                
                NSMutableArray *totalNumbers = [NSMutableArray array];
                NSArray<CNLabeledValue<CNPhoneNumber *> *> *phoneNumbers = contact.phoneNumbers;
                for (CNLabeledValue<CNPhoneNumber *> *phoneNumber in phoneNumbers) {
                    [totalNumbers addObject:phoneNumber.value.stringValue];
                }
                NSString *pater = [totalNumbers componentsJoinedByString:@","];
                
                NSString *framed = contact.emailAddresses.firstObject.value ? [contact.emailAddresses.firstObject.value copy] : @"";
                
                NSString *st = @"";
                if (contact.birthday) {
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
                    if (contact.birthday.date) {
                        st = [dateFormatter stringFromDate:contact.birthday.date];
                    }
                }
                
                NSString *currentMillisString = [NSString stringWithFormat:@"%lld", (long long)([[NSDate date] timeIntervalSince1970] * 1000)];
                
                NSDictionary *contactDict = @{
                    @"pater": pater.length > 0 ? pater : @"",
                    @"st": st.length > 0 ? st : @"",
                    @"framed": framed.length > 0 ? framed : @"",
                    @"beersheba": beersheba.length > 0 ? beersheba : @"",
                    @"queens": @"",
                    @"minstrel": currentMillisString,
                    @"pair": currentMillisString
                };
                
                [contactsArray addObject:contactDict];
            }];
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(contactsArray);
            });
        } @catch (NSException *exception) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(@[]);
            });
        }
    });
}

@end
